﻿@model OT.Areas.Admin.Models.Tests
<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="fa fa-times"></i></button>
    <h3 id="myModalLabel">Create Test</h3>
</div>
<div class="modal-body">
    <div id="alert_modal">
        @Html.ValidationSummary()
        @Html.Hidden("QuetionIDs", null, new { id = "QuetionIDs" })
          @Html.Hidden("StudentIDs", null, new { id = "StudentIDs" })
    </div>
    <div class="form-vertical" role="form">
        <div class="form-group">
            <div class="col-lg-13">
                <div id="divMsgCre"></div>
            </div>
        </div>
    </div>
    <div class="form-vertical" role="form">
        <div class="form-group" id="divCategoryName">
            <label class="col-lg-2 control-label" style="text-align: right">Category Name</label>
            <div class="col-lg-4">
                @Html.DropDownListFor(model => model.CategoryID, new SelectList(Model.cat, "CategoryID", "CategoryName"), "--- Select ---", new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.CategoryID)
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-2 control-label" style="text-align: right">Available Question</label>
            <div class="col-lg-4">
                <div id="TotAvail" style="color: green; font-size: 16px;"></div>
            </div>
        </div>
    </div>
    <div class="clearfix"></div>
    <div class="form-vertical" role="form">
        <div class="form-group" id="divSubject">
            <label class="col-lg-2 control-label" style="text-align: right">Subject</label>
            <div class="col-lg-4">
                <select id="SubjectID" class="form-control">
                    <option value="0">--Select--</option>
                </select>
            </div>
        </div>
        <div class="form-group" id="divTotalQuestion">
            <label class="col-lg-2 control-label" style="text-align: right">Total Question</label>
            <div class="col-lg-2">
                <input class="form-control" type="text" placeholder=" No of Question" id="TotalQuestion" maxlength="2">
            </div>
            <div class="col-lg-2" style="text-align: left; padding: 0; margin: 0;">
                <a href="/Instructor/Test/ShowAllQuestionCreate?subjectId=" id="Custom">Custom Question</a>
            </div>
        </div>
    </div>
    <div class="clearfix"></div>
    <div class="form-vertical" role="form">
        <div class="form-group" id="divTopic">
            <label class="col-lg-2 control-label" style="text-align: right">Topic</label>
            <div class="col-lg-4">
                <select id="TopicID" class="form-control">
                    <option value="0">--Select--</option>
                </select>
            </div>
        </div>
        <div class="form-group" id="divPassingScore">
            <label class="col-lg-2 control-label" style="text-align: right">Passing Score</label>
            <div class="col-lg-4">
                <input class="form-control" type="text" placeholder="Passing Score for Test" id="PassingScore" maxlength="3">
            </div>
        </div>
    </div>
    <div class="clearfix"></div>
    <div class="form-vertical" role="form">
        <div class="form-group" id="divTestType">
            <label class="col-lg-2 control-label" style="text-align: right">Test Type</label>
            <div class="col-lg-4">
                @Html.DropDownListFor(model => model.TestTypeID, new SelectList(Model.TestTypes, "TestTypeID", "TestType1"), "-- Select --", new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.TestTypeID)
            </div>
        </div>
        <div class="form-group" id="divDuration">
            <label class="col-lg-2 control-label" style="text-align: right">Duration </label>
            <div class="col-lg-4">
                <input class="form-control" type="text" placeholder="Duration of Exam [in Minute]" id="Duration" maxlength="3">
            </div>
        </div>
    </div>
    <div class="clearfix"></div>
    <div class="form-vertical" role="form">
        <div class="form-group" id="divTestName">
            <label class="col-lg-2 control-label" style="text-align: right">Test Name</label>
            <div class="col-lg-4">
                <input class="form-control" type="text" placeholder="Enter Test Name" id="TestName" maxlength="30">
            </div>
        </div>
        <div class="form-group" id="divPositiveMark">
            <label class="col-lg-2 control-label" style="text-align: right">Positive Mark</label>
            <div class="col-lg-4">
                <input class="form-control" type="text" placeholder="Positive Mark ex.1,2,3..." id="PositiveMark" maxlength="3">
            </div>
        </div>
    </div>
    <div class="clearfix"></div>
    <div class="form-vertical" role="form">
        <div class="form-group" id="divDescription">
            <label class="col-lg-2 control-label" style="text-align: right">Description</label>
            <div class="col-lg-4">
                <textarea class="form-control" placeholder="Enter Description" id="Description" rows="1" maxlength="50"></textarea>
            </div>
        </div>
        <div class="form-group" id="divNegativeMark">
            <label class="col-lg-2 control-label" style="text-align: right">Negative Mark</label>
            <div class="col-lg-4">
                <input class="form-control" type="text" placeholder="Negative Mark ex.1,2,3,..." id="NegativeMark" maxlength="3">
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-2 control-label" style="text-align: right">Status</label>
            <div class="col-lg-1">
                <input id="st" name="st" type="checkbox" class="form-control" />
            </div>
        </div>
    </div>
    
    <div class="clearfix"></div>
    <div class="form-vertical" role="form">
        <div class="form-group" id="d">
            <label class="col-lg-2 control-label" style="text-align: right">Select Students</label>
            <div class="col-lg-2">
                <a href="/Admin/Test/GetAllStudent" id="Students">Custom Students</a>
            </div>
            <label class="label label-info" style="font-size: 16px;" id="lblstudentcnt">0</label>
        </div>
    </div>
    <div class="clearfix"></div>
</div>
<div class="modal-footer">
    <button class="btn btn-default" type="submit" id="btnCreate"><i class="icon-cog"></i>Save</button>
    <button class="btn btn-default" data-dismiss="modal" aria-hidden="true"><i class="icon-off"></i>Cancel</button>
</div>

<script type="text/javascript">
    $("#btnCreate").click(function () {
        var TestTypeID = $("#TestTypeID option:selected").val();
        var CategoryID = $("#CategoryID option:selected").val();
        var SubjectID = $("#SubjectID option:selected").val();
        var TopicID = $("#TopicID option:selected").val();
        var TestName = $("#TestName").val();
        var Description = $("#Description").val();
        var TotalQuestion = $("#TotalQuestion").val();
        var Duration = $("#Duration").val();
        var PassingScore = $("#PassingScore").val();
        var PositiveMark = $("#PositiveMark").val();
        var NegativeMark = $("#NegativeMark").val();
        var QuestionIDs = $("#QuetionIDs").val();
        var StudentIDs = $("#StudentIDs").val();
        if ($("#st").is(':checked')) {
            Status = true;
        }
        else {
            Status = false;
        }

        if (TestTypeID == 0 || TestName == '' || CategoryID == 0 || SubjectID == 0 || TopicID == 0 || TotalQuestion == '' || Duration == '' || PassingScore == '' || PositiveMark == '' || NegativeMark == '') {
            showMsg('divMsgCre', 'Please Enter Value in Required Fields', 'danger');
            (TestTypeID == 0) ? $('#divTestType').addClass('has-error') : $('#divTestType').removeClass('has-error');
            (CategoryID == 0) ? $('#divCategoryName').addClass('has-error') : $('#divCategoryName').removeClass('has-error');
            (SubjectID == 0) ? $('#divSubject').addClass('has-error') : $('#divSubject').removeClass('has-error');
            (TopicID == 0) ? $('#divTopic').addClass('has-error') : $('#divTopic').removeClass('has-error');
            (TestName == '') ? $('#divTestName').addClass('has-error') : $('#divTestName').removeClass('has-error');
            (TotalQuestion == '') ? $('#divTotalQuestion').addClass('has-error') : $('#divTotalQuestion').removeClass('has-error');
            (Duration == '') ? $('#divDuration').addClass('has-error') : $('#divDuration').removeClass('has-error');
            (PassingScore == '') ? $('#divPassingScore').addClass('has-error') : $('#divPassingScore').removeClass('has-error');
            (PositiveMark == '') ? $('#divPositiveMark').addClass('has-error') : $('#divPositiveMark').removeClass('has-error');
            (NegativeMark == '') ? $('#divNegativeMark').addClass('has-error') : $('#divNegativeMark').removeClass('has-error');
        }
        else {
            var totavail = Number($("#TotAvail").text());
            var selectque = Number($('#TotalQuestion').val());
            if (selectque > totavail) {
                showMsg('divMsgCre', 'Total Question is Greater than Available Question', 'danger');
            }
            else {
                $.ajax({
                    url: "/admin/Test/Create?TestName=" + TestName + "&TestTypeID=" + TestTypeID + "&CategoryID=" + CategoryID + "&SubjectID=" + SubjectID + "&TopicID=" + TopicID + "&Description=" + Description + "&TotalQuestion=" + TotalQuestion + "&Duration=" + Duration + "&PassingScore=" + PassingScore + "&PositiveMark=" + PositiveMark + "&NegativeMark=" + NegativeMark + "&Status=" + Status + "&QuestionIDs=" + QuestionIDs + "&StudentIDs=" + StudentIDs,
                    type: "POST",
                    success: function (result) {
                        if (result != "NotValid") {
                            $('#modalDiv').modal('hide');
                            showMsg('divMsgIndex', 'Record  Saved Successfuly!', 'success');
                            loadPage(1, 10, '');
                        }
                        else {
                            showMsg('divMsgCre', 'Record Not Saved !!Error', 'danger');
                        }
                    },
                    error: function (data) {
                    }
                });
            }
        }
        return false;
    });

    $("#CategoryID").change(function (e) {
        var CategoryID = $(this).val();
        var sub = $("#SubjectID").empty();
        $.getJSON("/Admin/Topic/LoadSubject", { CategoryID: CategoryID },
               function (classesData) {
                   var select = $("#SubjectID");
                   select.empty();
                   select.append($('<option/>', {
                       value: 0,
                       text: "----Select Subject----"
                   }));
                   $.each(classesData, function (index, itemData) {

                       select.append($('<option/>', {
                           value: itemData.Value,
                           text: itemData.Text
                       }));
                   });
               });
        return false;
    });

    $("#SubjectID").change(function (e) {
        var SubjectID = $(this).val();
        var sub = $("#TopicID").empty();
        $.getJSON("/Admin/Test/LoadTopic", { SubjectID: SubjectID },
               function (classesData) {
                   var select = $("#TopicID");
                   select.append($('<option/>', {
                       value: 0,
                       text: "----Select Topic----"
                   }));
                   $.each(classesData, function (index, itemData) {

                       select.append($('<option/>', {
                           value: itemData.Value,
                           text: itemData.Text
                       }));
                   });
               });
        return false;
    });

    $("#TopicID").change(function (e) {
        var SubjectID = $("#SubjectID").val();
        var TopicID = $("#TopicID").val();
        $("#totalAvailQue").remove();
        var tot = $("#TotalQuestion").prop('disabled', false);

        $.getJSON("/Admin/Test/AvailQue", { SubjectID: SubjectID, TopicID: TopicID },
             function (classesData) {
                 $("#TotAvail").css({ "color": "red" });
                 $("#TotAvail").html(classesData);
             });
        return false;
    });

    $("#TotalQuestion").keypress(function (e) {
        if ((e.which < 48 || e.which > 57)) {
            if (e.which == 8 || e.which == 46 || e.which == 0) {
                $("#ErrorMsg").css({ "display": "none" });
                return true;
            }
            else {
                $("#ErrorMsg").css({ "display": "initial" });
                showMsg('divMsgCre', 'Enter only numbers 0-9', 'info');
                $("#ErrorMsg").html("Enter only numbers 0-9");
                return false;
            }
        }
    });

    $("#Duration").keypress(function (e) {
        if ((e.which < 48 || e.which > 57)) {
            if (e.which == 8 || e.which == 46 || e.which == 0) {
                $("#ErrorMsg1").css({ "display": "none" });
                return true;
            }
            else {
                $("#ErrorMsg1").css({ "display": "initial" });
                $("#ErrorMsg1").html("Enter only numbers 0-9");
                showMsg('divMsgCre', 'Enter only numbers 0-9', 'info');
                return false;
            }
        }
    });

    $("#PassingScore").keypress(function (e) {
        if ((e.which < 48 || e.which > 57)) {
            if (e.which == 8 || e.which == 46 || e.which == 0) {
                $("#ErrorMsg2").css({ "display": "none" });
                return true;
            }
            else {
                $("#ErrorMsg2").css({ "display": "initial" });
                //showMsg('divMsgCre', 'Enter only numbers 0-9', 'info');
                return false;
            }
        }
    });

    $("#PositiveMark").keypress(function (e) {
        if ((e.which < 48 || e.which > 57)) {
            if (e.which == 8 || e.which == 46 || e.which == 0) {
                $("#ErrorMsg3").css({ "display": "none" });
                return true;
            }
            else {
                $("#ErrorMsg3").css({ "display": "initial" });
                showMsg('divMsgCre', 'Enter only numbers 0-9', 'info');
                return false;
            }
        }
    });

    $("#NegativeMark").keypress(function (e) {
        if ((e.which < 48 || e.which > 57)) {
            if (e.which == 8 || e.which == 46 || e.which == 0) {
                $("#ErrorMsg4").css({ "display": "none" });
                return true;
            }
            else {
                $("#ErrorMsg4").css({ "display": "initial" });
                showMsg('divMsgCre', 'Enter only numbers 0-9', 'info');
                return false;
            }
        }
    });
    $("#Custom").click(function (e) {
        var id = $("#SubjectID option:selected").val();
        var tid = $("#TopicID option:selected").val();
        e.preventDefault();
        if (id == 0 || tid == 0) {
            showMsg('divMsgCre', 'Please Select Subject & Topic', 'danger');
        }
        else {
            $("#CustomQuestion").load(this.href + id + "&topicid=" + tid, function () {
                $('#modalQsDiv').modal({
                    backdrop: 'static'
                }, 'show');
            });
        }
    });
    $("#Students").click(function (e) {
        e.preventDefault();
        $("#CustomStudent").load(this.href, function () {
            $('#modalUserDiv').modal({
                backdrop: 'static'
            }, 'show');
        });
    });
</script>
